/**
 * @action对象生成文件
 * 
 * 一个页面可以抛出多个export
 * 但是只可以抛出一个export default
 */
import axios from 'axios'
import '@/mock/index'

 export const getInitData = (data) => {
   return {
     type: 'init_cart_data',
     data
   }
 }

 export const initCartData = () => {
    return (dispatch) => {
      axios.get('/api/initdata').then(res => {
        const action = getInitData(res.data)
        dispatch(action)
      })
    }
 }

 // 加入购物车函数
 export const getToCart = (item) => {
    return {
      type: 'to_cart',
      item
    }
 }

 export const getAddCartItem = (index) => {
   return {
     type: 'add_cart_item',
     index
   }
 }
 export const getRemoveCartItem = (index) => {
  return {
    type: 'remove_cart_item',
    index
  }
}

// 提交给redux的actio对象
export const getSwiperData = (data) => {
  return {
    type: 'init_banner_img',
    data
  }
}

// 通过axios获取后台数据 
export const initSwiperData = () => {
  return (dispatch) => {
    axios.get('/api/banner').then(res => {
      const action = getSwiperData(res.data)
      dispatch(action)
    })
  }
}